package xyz.zq.sf.common.log.aspect;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import xyz.zq.sf.common.core.util.SpringContextHolder;
import xyz.zq.sf.common.log.annotation.SysLog;
import xyz.zq.sf.common.log.event.SysLogEvent;
import xyz.zq.sf.common.log.util.SysLogUtil;

/**
 * 日志拦截器
 *
 * @author hzq
 * @date 2021-02-04 13:18
 */
@Slf4j
@Aspect
public class SysLogAspect {


    @Around("@annotation(sysLog)")
    @SneakyThrows
    public Object around(ProceedingJoinPoint point, SysLog sysLog) {
        /*类名称*/
        String strClassName = point.getTarget().getClass().getName();
        /*方法名称*/
        String strMethodName = point.getSignature().getName();
        log.debug("[类名]:{},[方法名]:{}", strClassName, strMethodName);
        /*保存日志*/
        log.info("客户端：{}，用户：{}", SysLogUtil.getClientId(), SysLogUtil.getUsername());

        Object object;
        try {
            object = point.proceed();
        } catch (Exception e) {
            throw e;
        } finally {
            /*发布事件*/
            SpringContextHolder.publishEvent(new SysLogEvent(sysLog));
        }
        return object;
    }

}
